package com.zbkj.admin.controller.publicly;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.admin.task.statistics.StatementTask;
import com.zbkj.admin.task.statistics.SummaryFinancialTask;
import com.zbkj.common.constants.OnePassConstants;
import com.zbkj.common.constants.SmsConstants;
import com.zbkj.common.exception.AppException;
import com.zbkj.common.request.ProductOrderSearchRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.service.service.StatisticsTaskService;
import com.zbkj.service.service.UserDividendRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * 缓存控制器
 +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/publicly/test")
@Api(tags = "任务测试控制器")
public class TaskTestController {

    @Autowired
    private StatementTask statementTask;
    @Autowired
    private SummaryFinancialTask summaryFinancialTask;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserDividendRecordService userDividendRecordService;

    @ApiOperation(value = "每日帐单定时任务")
    @RequestMapping(value = "/task/daily", method = RequestMethod.GET)
    public CommonResult<String> deleteByKey() {
        statementTask.dailyStatement();
        return CommonResult.success("成功");
    }

    @ApiOperation(value = "每月帐单定时任务")
    @RequestMapping(value = "/task/month", method = RequestMethod.GET)
    public CommonResult<String> taskMonth() {
        statementTask.monthStatement();
        return CommonResult.success("成功");
    }

    @ApiOperation(value = "每日财务流水汇总")
    @RequestMapping(value = "/task/financial", method = RequestMethod.GET)
    public CommonResult<String> summaryFinancialTask() {
        summaryFinancialTask.dailyStatement();
        return CommonResult.success("成功");
    }


    @ApiOperation(value = "每日财务流水汇总")
    @RequestMapping(value = "/task/test1", method = RequestMethod.GET)
    public CommonResult<String> tesdt1() {
        log.info("开始测试");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("expires_in", "3600");
        jsonObject.put("access_token", "");
        String accessToken;
        String accessToken1 = "test";
        Long expiresIn;
        accessToken = OnePassConstants.ONE_PASS_USER_TOKEN_PREFIX.concat(jsonObject.getString("access_token"));
        expiresIn = jsonObject.getLong("expires_in");
        expiresIn = expiresIn + CrmebDateUtil.getTime();
        redisUtil.set(StrUtil.format(OnePassConstants.ONE_PASS_TOKEN_KEY_PREFIX, accessToken1), accessToken, expiresIn, TimeUnit.SECONDS);

//        redisUtil.set(StrUtil.format(SmsConstants.SMS_VERIFICATION_PHONE_NUM, "aaa", "19520612698"), 1, 60L);

        return CommonResult.success("成功");
    }

    @ApiOperation(value = "全民分红任务测试")
    @RequestMapping(value = "/task/dividend", method = RequestMethod.GET)
    public CommonResult<String> dividendTest() {
        // 统计金额
        String startDateStr = "2025-11-01 00:00:00";
        String endDateStr = "2025-11-02 00:00:00";
        userDividendRecordService.userDividendTask(null,startDateStr,endDateStr);
        return CommonResult.success("成功");
    }

}
